/**
 * 
 */
package precise.server.standalone;

import java.io.File;
import java.util.PropertyResourceBundle;

import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.DefaultHandler;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;


/**
 * @author PEide3
 *
 */
public class StandaloneServer {

	public static void main(String[] args) /*throws FileNotFoundException, IOException*/ {
		
		
//		File file = new File ("conf/precise.properties");
//		System.out.println("File: " + file.getAbsolutePath());
////		ResourceBundle preciseProperties = ResourceBundle.getBundle("/precise");
////		preciseProperties.getString("precise.server.port");
//
//		PropertyResourceBundle properties = new PropertyResourceBundle (new FileReader(file));
//		
//		URL resource = StandaloneServer.class.getResource("precise.properties");
//		System.out.println("Resource - precise.properties:  " + resource);
//
//		resource = StandaloneServer.class.getResource("/precise.properties");
//		System.out.println("Resource - /precise.properties:  " + resource);
//		PropertyResourceBundle properties1 = new PropertyResourceBundle (resource.openStream());
//		System.out.println("Resource - /precise.properties - port:  " + properties1.getString("precise.server.port"));
//
//		properties1 = (PropertyResourceBundle)PropertyResourceBundle.getBundle("precise");
//		System.out.println("Resource from PropertyResourceBundle - precise - port:  " + properties1.getString("precise.server.port"));
//
//		resource = StandaloneServer.class.getResource("precise");
//		System.out.println("Resource - precise:  " + resource);
		
		PropertyResourceBundle properties = (PropertyResourceBundle)PropertyResourceBundle.getBundle("precise");
		
		int port = new Integer (properties.getString("precise.server.port")).intValue();
		String contextPath = properties.getString("precise.server.contextpath");
		Server service = new Server() ;

		Connector connector = new SelectChannelConnector();
        connector.setPort(port);
        service.setConnectors(new Connector[]{connector});
        
//        System.setProperty("install_hibernate_cfg", new File ("conf/hibernate.cfg.xml").getAbsolutePath());

        WebAppContext webappcontext = new WebAppContext();
        webappcontext.setContextPath("/" + contextPath); //TODO: extract to properties file which is filtered by maven..
        webappcontext.setWar("lib/precise-web-1.0-SNAPSHOT.war");

        HandlerCollection handlers= new HandlerCollection();
        handlers.setHandlers(new Handler[]{webappcontext, new DefaultHandler()});

        service.setHandler(handlers);

        try {
			service.start();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        try {
			service.join();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
        
	}
}
